﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Minecraft.Data.NBT;

namespace Minecraft.Data {
    public class World : ITagConverter {

        public long Time;
        public DateTime LastPlayed;
        public Player Player;
        public int SpawnX;
        public int SpawnY;
        public int SpawnZ;
        public long SizeOnDisk;
        public long RandomSeed;

        public static World Load(string filename) {
            Tag root = NBTReader.LoadNBT(filename);
            return new World(root); ;
        }

        public World() { }
        public World(Tag root) {
            Tag data = root.Children[0];

            foreach (var child in data.Children) {
                switch (child.Name) {
                    case "Time":
                        this.Time = (long)child.Payload;
                        break;

                    case "LastPlayed":
                        DateTime d1970 = new DateTime(1970, 1, 1);
                        this.LastPlayed = d1970.Add(TimeSpan.FromMilliseconds((long)child.Payload)).ToLocalTime();
                        break;

                    case "SpawnX":
                        this.SpawnX = (int)child.Payload;
                        break;

                    case "SpawnY":
                        this.SpawnY = (int)child.Payload;
                        break;

                    case "SpawnZ":
                        this.SpawnZ = (int)child.Payload;
                        break;

                    case "SizeOnDisk":
                        this.SizeOnDisk = (long)child.Payload;
                        break;

                    case "RandomSeed":
                        this.RandomSeed = (long)child.Payload;
                        break;

                    case "Player":
                        this.Player = new Player(child);
                        break;

                    default:
                        break;
                }
            }
        }

        public void Save(String filename) {
            Tag data = this.ToTag();
            NBTWriter.SaveNBT(filename, data);
        }

        public Tag ToTag() {
            Tag root = new Tag() {
                Type = TagType.Compound
            };

            root.Children.Add(new Tag() { Type = TagType.Compound, Name = "Data" });
            root.Children[0].Children.Add(new Tag() { Type = TagType.Long, Name = "Time", Payload = this.Time });
            root.Children[0].Children.Add(new Tag() { Type = TagType.Long, Name = "LastPlayed", 
                Payload = Convert.ToInt64((this.LastPlayed - new DateTime(1970, 1, 1)).TotalSeconds) });
            root.Children[0].Children.Add(this.Player.ToTag());
            root.Children[0].Children.Add(new Tag() { Type = TagType.Int, Name = "SpawnX", Payload = this.SpawnX });
            root.Children[0].Children.Add(new Tag() { Type = TagType.Int, Name = "SpawnY", Payload = this.SpawnY });
            root.Children[0].Children.Add(new Tag() { Type = TagType.Int, Name = "SpawnZ", Payload = this.SpawnZ });
            root.Children[0].Children.Add(new Tag() { Type = TagType.Long, Name = "SizeOnDisk", Payload = this.SizeOnDisk });
            root.Children[0].Children.Add(new Tag() { Type = TagType.Long, Name = "RandomSeed", Payload = this.RandomSeed });

            return root;
        }
    }
}
